﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Controls;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Windows;
using System.Collections.ObjectModel;
using WebServerConnectorDll;

namespace FridgeManagerApp
{
    public class StockNoteViewModel : DataListViewModel
    {
        private UserControl gWindow;
        private ParentWindowViewModel gParentWindowViewModel;
        private StockNoteModel gStockNoteModel;
        

        public StockNoteViewModel(UserControl pWindow, ParentWindowViewModel pParentWindowViewModel)
        {
            gWindow = pWindow;
            gParentWindowViewModel = pParentWindowViewModel;
            gStockNoteModel = new StockNoteModel();

            UpdateList();
        }

        public void UpdateList()
        {
            DataList.Clear();

            
            HttpResponse response = FridgeConnector.PostMessage(EStudy.StockNote, EMethod.SelectAll, "");
            if (response.Status == "Success")
            {
                List<WStockNote> list = JSON.parse<List<WStockNote>>(response.Data1);
                for (int i = 0; i < list.Count; i++)
                {
                    LStockNote item = new LStockNote();
                    item.IsChoose = false;
                    item.StockUID = list[i].StockUID;
                    item.DominoTypeUID = list[i].DominoTypeUID;
                    item.DominoUID = list[i].DominoUID;
                    item.OfficeUID = list[i].OfficeUID;
                    item.PersonnelUID = list[i].PersonnelUID;
                    item.Price = list[i].Price;
                    item.CreateTime = list[i].CreateTime;
                    DataList.Add(item);
                }
            }
        }

        public ObservableCollection<LStockNote> DataList
        {
            get
            {
                return gStockNoteModel.DataList;
            }

            set
            {
                DataList = value;
                RaisePropertyChanged("DataList");
            }
        }

        public ObservableCollection<LStockNote> DataListEdit
        {
            get
            {
                return gStockNoteModel.DataListEdit;
            }

            set
            {
                DataListEdit = value;
                RaisePropertyChanged("DataListEdit");
            }
        }

        public Visibility DataListEditVisibility
        {
            get
            {
                return gStockNoteModel.dataListEditVisibility;
            }
            set
            {
                gStockNoteModel.dataListEditVisibility = value;
                RaisePropertyChanged("DataListEditVisibility");
            }
        }

        protected override void DLCloseCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLSaveCommand(object obj)
        {
            String postData = null;
            List<WStockNote> list = new List<WStockNote>();
            foreach (LStockNote item in DataListEdit)
            {
                if (item.IsChoose)
                {
                    list.Add(new WStockNote(item));
                }
            }
            postData = JSON.stringify(list);
            HttpResponse response = FridgeConnector.PostMessage(EStudy.StockNote, EMethod.Update, postData);

            UpdateList();

            DataListEditVisibility = Visibility.Hidden;
            DLBSaveVisibility = Visibility.Hidden;
            EditMode = false;
        }

        protected override void DLQuitCommand(object obj)
        {
            gParentWindowViewModel.RemoveUserControl(gWindow.Uid);
        }

        protected override void DLGoCommand(object obj)
        {

        }

        protected override void DLPageUpCommand(object obj)
        {

        }

        protected override void DLPageDownCommand(object obj)
        {

        }

        protected override void DLCheckedCommand(object obj)
        {
            if (DLTIsChecked)
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = true;
                }
            }
            else
            {
                foreach (var item in DataList)
                {
                    item.IsChoose = false;
                }
            }
        }

        protected override void DLAddCommand(object obj)
        {
            //PersonnelInsert window = new PersonnelInsert();
            //PersonnelInsertViewModel viewModel = new StockNoteInsertViewModel(window, this);
            //window.DataContext = viewModel;
            //window.ShowDialog();
        }

        protected override void DLEditCommand(object obj)
        {
            if(!EditMode)
            {
                DataListEdit.Clear();

                foreach (LStockNote item in DataList)
                {
                    if(item.IsChoose)
                    {
                        DataListEdit.Add(item);
                    }
                }

                if(DataListEdit.Count>0)
                {
                    DataListEditVisibility = Visibility.Visible;
                    DLBSaveVisibility = Visibility.Visible;
                    EditMode = true;
                }
            }
        }

        protected override void DLDeleteCommand(object obj)
        {
            String postData = null;
            List<WStockNote> list = new List<WStockNote>();
            foreach (LStockNote item in DataList)
            {
                if (item.IsChoose)
                {
                    list.Add(new WStockNote(item));
                }
            }
            postData = JSON.stringify(list);
            HttpResponse response = FridgeConnector.PostMessage(EStudy.StockNote, EMethod.Delete, postData);

            UpdateList();
        }
    }
}
